package com.ruoyi.com.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.com.domain.ComDepartment;
import com.ruoyi.com.domain.vo.DepartmentRoleDto;
import com.ruoyi.com.mapper.ComDepartmentMapper;
import com.ruoyi.com.service.IComDepartmentService;
import com.ruoyi.common.core.domain.OperateBasicForm;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysDepartmentRole;
import com.ruoyi.system.mapper.SysDepartmentRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 公共部门Service业务层处理
 *
 * @author sunye
 * @date 2024-09-29
 */
@Service
public class ComDepartmentServiceImpl extends ServiceImpl<ComDepartmentMapper, ComDepartment> implements IComDepartmentService
{
    @Autowired
    private ComDepartmentMapper comDepartmentMapper;

    @Autowired
    private SysDepartmentRoleMapper sysDepartmentRoleMapper;

    /**
     * 查询公共部门
     *
     * @param departmentId 公共部门主键
     * @return 公共部门
     */
    @Override
    public ComDepartment selectComDepartmentByDepartmentId(Long departmentId)
    {
        return comDepartmentMapper.selectComDepartmentByDepartmentId(departmentId);
    }

    /**
     * 查询公共部门列表
     *
     * @param comDepartment 公共部门
     * @return 公共部门
     */
    @Override
    public List<ComDepartment> selectComDepartmentList(ComDepartment comDepartment)
    {
        return comDepartmentMapper.selectComDepartmentList(comDepartment);
    }

    /**
     * 新增公共部门
     *
     * @param comDepartment 公共部门
     * @return 结果
     */
    @Override
    public int insertComDepartment(ComDepartment comDepartment)
    {
        comDepartment.setCreateTime(DateUtils.getNowDate());
       comDepartment.setUpdateTime(DateUtils.getNowDate());
        return comDepartmentMapper.insert(comDepartment);
    }

    /**
     * 修改公共部门
     *
     * @param comDepartment 公共部门
     * @return 结果
     */
    @Override
    public int updateComDepartment(ComDepartment comDepartment)
    {
        comDepartment.setUpdateTime(DateUtils.getNowDate());
        return comDepartmentMapper.updateById(comDepartment);
    }

    /**
     * 批量删除公共部门
     *
     * @param departmentIds 需要删除的公共部门主键
     * @return 结果
     */
    @Override
    public int deleteComDepartmentByDepartmentIds(Long[] departmentIds)
    {
        return comDepartmentMapper.deleteComDepartmentByDepartmentIds(departmentIds);
    }

    /**
     * 删除公共部门信息
     *
     * @param departmentId 公共部门主键
     * @return 结果
     */
    @Override
    public int deleteComDepartmentByDepartmentId(Long departmentId)
    {
        return comDepartmentMapper.deleteComDepartmentByDepartmentId(departmentId);
    }
    /**
     * 置为有效/无效公共部门
     *
     */
    @Override
    public int effectiveStatus(OperateBasicForm operateBasicForm)
    {
    return comDepartmentMapper.effectiveStatus(operateBasicForm);
    }

    @Override
    public boolean hasChildByDepartmentId(Long departmentId) {
        int result = comDepartmentMapper.hasChildByDepartmentId(departmentId);
        return result > 0;
    }


    @Override
    public List<DepartmentRoleDto> selectDepartmentRole(Long departmentId) {
        return comDepartmentMapper.selectDepartmentRole(departmentId);
    }

    @Override
    public int bindDepartmentRole(ComDepartment comDepartment) {
        Long[] roleIds = comDepartment.getRoleIds();
        //绑定部门和角色的关系
        if (StringUtils.isNotEmpty(roleIds))
        {
            List<SysDepartmentRole> list = new ArrayList<SysDepartmentRole>(roleIds.length);
            for (Long roleId : roleIds)
            {
                SysDepartmentRole ur = new SysDepartmentRole();
                ur.setDepartmentId(comDepartment.getDepartmentId());
                ur.setRoleId(roleId);
                ur.setSort(comDepartment.getSort());
                list.add(ur);
            }
            // 删除用户与角色关联
            sysDepartmentRoleMapper.deleteUserRoleByDepartmentId(comDepartment.getDepartmentId());
            sysDepartmentRoleMapper.batchDepartmentRole(list);
        }else{
            sysDepartmentRoleMapper.deleteUserRoleByDepartmentId(comDepartment.getDepartmentId());
        }
        return 1;
    }


}
